package com.lanyou.esb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_ESB_BU_IF_TRACE_LOG")
public class ServiceLogTrace{
	
	private String id;
	private Http http;
	private String appSystem;
	private String message;
	/** 创建时间 **/
	private Date createdDate;
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "TRACE_LOG_ID", length = 36)
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name = "IF_HTTP_ID")
	public Http getHttp() {
		return http;
	}
	public void setHttp(Http http) {
		this.http = http;
	}
	
	@Column(name="CALL_SYSTEM_CODE")
	public String getAppSystem() {
		return appSystem;
	}
	
	public void setAppSystem(String appSystem) {
		this.appSystem = appSystem;
	}
	
	@Column(name="TRACE_MSG")
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	@Column(name = "CREATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	public Date getCreatedDate() {
		return createdDate;
	}
	
	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	@Transient
	public String getInterfaceName() {
		try {
			return (String) PropertyUtils.getProperty(http, "name");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getSystemName() {
		try {
			return (String) PropertyUtils.getProperty(http, "fieldSystem.name");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getFieldName() {
		try {
			return (String) PropertyUtils.getProperty(http, "fieldSystem.field.name");
		} catch (Exception e) {
			return null;
		}
	}
	
}